package com.hyj.jpdy.grap.html;



import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import com.hyj.jpdy.grap.html.bean.HtmlInfoVO;
import com.hyj.jpdy.grap.html.bean.HtmlQO;
import com.hyj.util.HttpUtil;


public class GrapHtmlUtil {

	/**
	 * 只获取纯粹的文字
	 * @param qo
	 * @return
	 */
	public static HtmlInfoVO getContent(HtmlQO qo){
		HtmlInfoVO vo   = null;
		try {
			String url = qo.getUrl();
			url = urlEncode(url,"UTF-8");
			
			String html =  HttpUtil.getRemoteResp(url,qo.getCharset());
			Document  doc = Jsoup.parse(html);
		    vo = new HtmlInfoVO();
		    
		    
		  //正文
			//String info1 = Jsoup.clean(sinaNew.getElementById("artibody").html(), Whitelist.basic());
		    Element content =  doc.getElementById(qo.getContentId());
			if(content!=null){
				vo.setHtmlContent(content.html());
				vo.setTextContent(content.text());
				
				Elements infos =  content.getElementsByTag("p");
				if(infos==null||infos.size()<=0){
					return null;
				}
				List<String> list = new ArrayList<String>();
				for(Element text :infos){
					String ss = text.text();
					list.add(ss);
				} 
				vo.setContent(list);
			}
			
			//创建时间
			vo.setGrapDate(new Date());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return vo;
	}
	
	/**
	 * 获取HTML 界面的内容，过滤JS，广告，只保留正文
	 * @return
	 */
	public static HtmlInfoVO getHtmlContent(HtmlQO qo){
		HtmlInfoVO vo   = null;
		try {
			String url = qo.getUrl();
			url = urlEncode(url,"UTF-8");
			
			String html =  HttpUtil.getRemoteResp(url,qo.getCharset());
			Document  doc = Jsoup.parse(html);
			/*Document  doc = Jsoup.connect(url).
			   header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0").
			   timeout(2000).get();*/
		    vo = new HtmlInfoVO();
		    
		    // 标题 获取 
		    String title = "";
		    if(HtmlQO.TYPE_ID.equals(qo.getTitleType())){
		       Element titleElement = doc.getElementById(qo.getTitleValue());
		       title = titleElement!=null ? titleElement.text() : "";
		    }else if(HtmlQO.TYPE_ATTR.equals(qo.getTitleType())){
		       Elements titleElement = doc.getElementsByAttributeValue(qo.getTitleKey(), qo.getTitleKey());
		       title = titleElement!=null ? titleElement.text() : "";
		    }else if(HtmlQO.TYPE_TAG.equals(qo.getTitleType())){
		    	Elements titleElement = doc.getElementsByTag(qo.getTitleValue());
			    title = titleElement!=null ? titleElement.text() : "";
		    }
		    vo.setTitle(title);
		    
		  //正文
			//String info1 = Jsoup.clean(sinaNew.getElementById("artibody").html(), Whitelist.basic());
		    Element content =  doc.getElementById(qo.getContentId());
			Elements infos =   content!=null  ? content.getElementsByTag("p") :null;
			if(infos==null||infos.size()<=0){
				return null;
			}
			List<String> list = new ArrayList<String>();
			for(Element text :infos){
				String ss = text.text();
				list.add(ss);
			} 
			vo.setContent(list);
			
			// 发布日期
			if(HtmlQO.TYPE_ID.equals(qo.getPubDateType())){
			   String date = doc.getElementById(qo.getPubDateValue()).text();
			   vo.setPubDate(date);
			}else if(HtmlQO.TYPE_ATTR.equals(qo.getPubDateType())){
			   String date = doc.getElementsByAttributeValue(qo.getPubDateKey(), qo.getPubDateValue()).text();
			   vo.setPubDate(date);
			}else if(HtmlQO.TYPE_TAG.equals(qo.getPubDateType())){
			    String date = doc.getElementsByTag(qo.getPubDateValue()).text();
			    vo.setPubDate(date);
			}
			
			//创建时间
			vo.setGrapDate(new Date());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return vo;
	}
	
	/**
	 * 
	 * @param url
	 * @return
	 */
	public static Document getDocument(String url,String charset ){
		Document doc =null;
		try {
			/*url = urlEncode(url,"UTF-8");
			doc = Jsoup.connect(url).
			   header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0").
			   timeout(2000).get();*/
			
			String html =  HttpUtil.getRemoteResp(url,charset);
			doc = Jsoup.parse(html);
		} catch (IOException e) {
			 
		}
		return doc;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 新浪模板
		/*String url = "http://news.sina.com.cn/c/2013-04-21/183526901323.shtml";
		String contentId ="artibody";
		
		String titleType = HtmlQO.TYPE_ID;
		String titleKey = "id";
		String titleValue = "artibodyTitle";
		
		String pubType = HtmlQO.TYPE_ID;
		String pubKey = "id";
		String pubValue = "pub_date";*/
		
		
		// 腾讯
		/*String url = "http://news.qq.com/a/20130421/001518.htm";
		String contentId ="Cnt-Main-Article-QQ";
		
		String titleType = HtmlQO.TYPE_TAG;
		String titleKey = "h1";
		String titleValue = "h1";
		
		String pubType = HtmlQO.TYPE_ATTR;
		String pubKey = "class";
		String pubValue = "article-time";*/
		
		//网易
		/*String url = "http://news.163.com/13/0418/23/8SPFRB030001121M.html";
		String contentId ="endText";
		String titleType = HtmlQO.TYPE_ID;
		String titleKey = "";
		String titleValue = "h1title";
		
		String pubType = HtmlQO.TYPE_ATTR;
		String pubKey = "class";
		String pubValue = "left";*/
		
		//笑话
		String url = "http://www.jokeji.cn/jokehtml/bxnn/20130424000722.htm";
		String contentId ="text110";
		String titleType = HtmlQO.TYPE_ATTR;
		String titleKey = "class";
		String titleValue = "mob_left_b2";
		
		String pubType = HtmlQO.TYPE_TAG;
		String pubKey = "class";
		String pubValue = "i";
		
		
		HtmlQO qo = new HtmlQO();
		qo.setCharset("GBK");
		qo.setUrl(url);
		qo.setContentId(contentId);
		qo.setTitleType(titleType);
		qo.setTitleKey(titleKey);
		qo.setTitleValue(titleValue);
		qo.setPubDateType(pubType);
		qo.setPubDateKey(pubKey);
		qo.setPubDateValue(pubValue);
		
		HtmlInfoVO vo =	getHtmlContent(qo);
		System.out.println("日期：  "+vo.getPubDate());
		System.out.println("标题：  "+vo.getTitle());
		List<String> list = vo.getContent();
		for(String str:list){
		   System.out.println(str);
		}
		
		
		 
	}
	
	public static String urlEncode(String url,String encode){
		String protocal = url.substring(0, 7);
		String uri = url.substring(7);
		String[] args = uri.split("/");
		String[] args2 = new String[args.length];
		for(int i = 0;i<args.length;i++){
			String s  = args[i];
			try {
				args2[i] =  URLEncoder.encode(s, encode);
			} catch (UnsupportedEncodingException e) {
				return "";
			}
		}
		;
		return protocal+StringUtils.arrayToDelimitedString(args2, "/");
	}

}